package chat.adapter;

import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;


import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.whu.administrator.ws.MyApplication;
import com.whu.administrator.ws.R;

import java.util.ArrayList;
import java.util.List;

import chat.network.http.AgreeAddContact;
import chat.network.http.GetFriendRequest;
import inter.Delegate;
import inter.ResponseStatus;
import utils.TimeUtil;

import static com.whu.administrator.ws.R.id.id_item_tv_id;

/**
 * 好友请求
 * Created by 凯 on 2015/10/28.
 */
public class FriendRequstAdapter extends BaseAdapter {

    Delegate agreeResponse = new Delegate() {
        @Override
        public void onResponse(String response) {
            if (!response.startsWith("{")) return;
            Gson gson = new Gson();

            AgreeAddContact.ResponseAgree responseAgree = gson.fromJson(response, AgreeAddContact.ResponseAgree.class);
            if (responseAgree.getStatus().equals(ResponseStatus.SUCCESS))
            {
                Toast.makeText(MyApplication.share(),"同意好用请求成功",Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onErrorResponse(VolleyError error) {

        }
    };


    List<GetFriendRequest.NewFriends> newFriends = new ArrayList<GetFriendRequest.NewFriends>();

    public void setNewFriends(List<GetFriendRequest.NewFriends> newFriends) {
        this.newFriends = newFriends;
    }

    @Override
    public int getCount() {
        return newFriends.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final GetFriendRequest.NewFriends newFriend = newFriends.get(position);
        convertView = View.inflate(MyApplication.share(), R.layout.item_friendrequest, null);
        TextView tv_time = (TextView) convertView.findViewById(R.id.id_tv_time);
        TextView tv_id = (TextView) convertView.findViewById(id_item_tv_id);
        TextView tv_nickname = (TextView) convertView.findViewById(R.id.id_tv_nickName);
        TextView tv_realName = (TextView) convertView.findViewById(R.id.id_tv_realName);
        TextView tv_content = (TextView) convertView.findViewById(R.id.id_tv_content);

        tv_time.setText(newFriend.getSub_Time());
        tv_id.setText(newFriend.getUser_ID());
        tv_nickname.setText(newFriend.getUser_NickName());
        tv_realName.setText(newFriend.getUser_RealName());
        tv_content.setText(newFriend.getSub_Desc());

        Button btn_agree = (Button) convertView.findViewById(R.id.id_btn_agree);
        btn_agree.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AgreeAddContact agreeAddContact = new AgreeAddContact();
                agreeAddContact.setDelegate(agreeResponse);
                AgreeAddContact.AgreeContact agreeContact = new AgreeAddContact.AgreeContact();
                agreeContact.setInvite_from_ID(newFriend.getUser_ID());
                agreeContact.setInvite_to_ID(MyApplication.getOwnUser().getUser_ID());
                agreeContact.setSubscrip_Time(TimeUtil.currentTime());
                agreeAddContact.setAgreeContact(agreeContact);
                agreeAddContact.agree();
            }
        });


        return convertView;
    }


}
